/*
 * Copyright (c) 2018. J4dream created
 */

package com.j4dream.property.javabasic.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

// Callable 接口类似于Runnable，两者都是为那些其实例可能被另一个线程执行的类设计的。但是 Runnable 不会返回结果，并且无法抛出经过检查
// 的异常，而Callable是可返回结果并且可以抛出异常的任务
public class MyCallable implements Callable {
    @Override
    public Integer call() {
        int count = 0;
        for (int i = 0; i < 1000; i++) {
            //System.out.println(Thread.currentThread().getName() + ":" + i);
            count += i;
        }
        try {
            Thread.sleep(2000);
        } catch (Exception e) {

        }

        return count;
    }

    public static void main(String[] args) throws Throwable {
        ExecutorService pool = Executors.newFixedThreadPool(2);
        Future<Integer> future1 = pool.submit(new MyCallable());
        System.out.println(future1.get() + "");
        Future<Integer> future2 = pool.submit(new MyCallable());
        pool.shutdown();
        System.out.println(12);
        System.out.println(future1.get() + "");
        System.out.println(future2.get() + "");
    }


}
